import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
'''
# Read the CSV file
data = pd.read_csv("2signal_data.csv", names=["Time", "Signal"])

# Time-domain plot
plt.figure(figsize=(12, 6))
plt.plot(data["Time"], data["Signal"], label="Noisy Signal")
plt.title("Time Domain Signal")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.show()
'''
# Plot the original signal without noise
# Read the CSV file
data = pd.read_csv("2signal_data.csv", names=["Time", "Signal"])

# Calculate the original sine wave, assuming known frequency and amplitude
frequency = 10.0  # Sine wave frequency, same as the generated signal
amplitude = 1.0  # Sine wave amplitude, same as the generated signal
clean_signal = amplitude * np.abs(np.sin(2 * np.pi * frequency * data["Time"]))

# Time-domain plot of the noisy signal and the original sine wave
plt.figure(figsize=(12, 6))
plt.plot(data["Time"], data["Signal"], label="Noisy Signal")
plt.plot(data["Time"], clean_signal, label="Original Sin Wave", linestyle='--')
plt.title("Time Domain Signal Comparison")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.show()

# Implement DFT manually
def compute_dft(signal):
    N = len(signal)
    dft = []
    for k in range(N):
        real_part = sum(signal[n] * np.cos(2 * np.pi * k * n / N) for n in range(N))
        imag_part = -sum(signal[n] * np.sin(2 * np.pi * k * n / N) for n in range(N))
        dft.append(complex(real_part, imag_part))
    return np.array(dft)

# Retrieve the signal
signal = data["Signal"].values

# Compute DFT
dft_result = compute_dft(signal)

# Frequency and magnitude
N = len(signal)
Fs = 100.0  # Sampling rate
frequencies = np.linspace(0, Fs / 2, N // 2)
fft_magnitude = np.abs(dft_result[:N // 2]) / N

# Frequency-domain plot
plt.figure(figsize=(12, 6))
plt.plot(frequencies, fft_magnitude, label="Frequency Spectrum")
plt.title("Frequency Domain Signal")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.show()
